package com.szh.taobaounion.ui.fragment;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.szh.taobaounion.Presenter.impl.ISellPagePresenterImpl;
import com.szh.taobaounion.Presenter.impl.TicketPresenterImpl;
import com.szh.taobaounion.R;
import com.szh.taobaounion.base.BaseFragment;
import com.szh.taobaounion.model.domain.IBaseInfo;
import com.szh.taobaounion.model.domain.OnSellContent;
import com.szh.taobaounion.ui.activity.TicketActivity;
import com.szh.taobaounion.ui.adapter.OnSellContentAdapter;
import com.szh.taobaounion.utils.PresenterManager;
import com.szh.taobaounion.utils.SizeUtils;
import com.szh.taobaounion.utils.TicketUtil;
import com.szh.taobaounion.utils.ToastUtil;
import com.szh.taobaounion.view.IOnSellPageCallaBack;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class OnSellFragment extends BaseFragment implements IOnSellPageCallaBack, OnSellContentAdapter.OnSellClickListener {
   @BindView(R.id.on_sell_content_list)
   public RecyclerView mContentRv;

   @BindView(R.id.on_sell_refresh_layout)
   public TwinklingRefreshLayout mTwinklingRefreshLayout;

    @BindView(R.id.fragment_bar_title_tv)
    public TextView title;
    private ISellPagePresenterImpl mSellPagePresenter;
    private OnSellContentAdapter mOnSellContentAdapter;
    public static final int DEFAULT_SPAN_COUNT=2;

    @Override
    protected int getRootViewResId() {
        return R.layout.fragment_on_sell;
    }



    @Override
    protected void initListener() {
        super.initListener();
        mTwinklingRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                //加载更多内容
                if (mSellPagePresenter != null) {
                    mSellPagePresenter.LoaderMore();
                }
            }
        });
        mOnSellContentAdapter.setOnSellClickListener(this);
    }

    @Override
    protected View lodeRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_lwith_bar_ayout,container,false);
    }

    @Override
    protected void initView(View rootView) {
        title.setText("特惠宝贝");
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), DEFAULT_SPAN_COUNT);
        mContentRv.setLayoutManager(layoutManager);
        mOnSellContentAdapter = new OnSellContentAdapter();
        mContentRv.setAdapter(mOnSellContentAdapter);
        mContentRv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.top = SizeUtils.dip2px(getContext(),2.5f);
                outRect.bottom = SizeUtils.dip2px(getContext(),2.5f);
                outRect.left = SizeUtils.dip2px(getContext(),2.5f);
                outRect.right = SizeUtils.dip2px(getContext(),2.5f);
            }
        });
        mTwinklingRefreshLayout.setEnableLoadmore(true);
        mTwinklingRefreshLayout.setEnableRefresh(false);
        mTwinklingRefreshLayout.setEnableOverScroll(true);
    }

    @Override
    protected void initPresenter() {
        mSellPagePresenter = PresenterManager.getInstance().getmSellPagePresenter();
        mSellPagePresenter.registerViewCallBack(this);
        mSellPagePresenter.getOnSellContent();
    }

    @Override
    protected void release() {
        super.release();
        if (mSellPagePresenter != null) {
            mSellPagePresenter.unregisterViewCallBack(this);
        }
    }

    @Override
    public void onContentLoadedSuccess(OnSellContent result) {
        mOnSellContentAdapter.setData(result);
        setUpState(State.SUCCESS);
    }

    @Override
    public void onMoreLoaded(OnSellContent moreResult) {
        mTwinklingRefreshLayout.finishLoadmore();
          //添加到适配器
        mOnSellContentAdapter.onMoreLoaded(moreResult);
        ToastUtil.ShowToast("加载了"+moreResult.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data().size()+"条数据");

    }

    @Override
    public void retry() {
        mSellPagePresenter.reLoad();
    }

    @Override
    public void onMoreLoadedError() {
        mTwinklingRefreshLayout.finishLoadmore();
        ToastUtil.ShowToast("网络异常请稍后重试...");
    }

    @Override
    public void onMoreLoadedEmpty() {
        mTwinklingRefreshLayout.finishLoadmore();
        ToastUtil.ShowToast("没有更多内容了...");
    }

    @Override
    public void onError() {
      setUpState(State.ERROR);
    }

    @Override
    public void onLoading() {
       setUpState(State.LOADING);
    }

    @Override
    public void onEmpty() {
     setUpState(State.Empty);
    }

    @Override
    public void onSellItemClick(IBaseInfo item) {
//        String title = dataBean.getTitle();
//        //详情地址
//        String url = dataBean.getCoupon_click_url();
//        if (TextUtils.isEmpty(url)) {
//            url=dataBean.getClick_url();
//        }
//        String cover = dataBean.getPict_url();
//
//
//        TicketPresenterImpl ticketPresenter = PresenterManager.getInstance().getmTicketPresenter();
//        ticketPresenter.getTicket(title,url,cover);
//        startActivity(new Intent(getContext(), TicketActivity.class));
        TicketUtil.toTicketPage(getContext(),item);
    }
}
